package com.yc.spring.jdbc;

import com.yc.spring.jdbc.bean.Account;
import com.yc.spring.jdbc.bean.Record;
import com.yc.spring.jdbc.dao.AccountDao;
import com.yc.spring.jdbc.dao.OprecordDao;
import com.yc.zyz.service.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/4
 * Email haijunzhou@hnit.edu.cn
 */
@Service
@Transactional
public class BankBiz {

    @Autowired
    AccountDao accountDao;
    @Autowired
    OprecordDao oprecordDao;

    @Autowired
    BankBiz1 bankBiz1;

    public void deposit(int aid, double money){
        Account account = accountDao.selectById(aid);
        account.setBalance(account.getBalance() + money);
        accountDao.updateById(account);

//        int i = 1/0;

        Record record = new Record();
        record.setAid(aid);
        record.setMoney(money);
        oprecordDao.insert(record);
    }

    public void withdraw(int aid, double money) throws BizException {
        Account account = accountDao.selectById(aid);
        if(account.getBalance()<money){
            throw new BizException("余额不足!");
        }
        account.setBalance(account.getBalance() - money);
        accountDao.updateById(account);
        Record record = new Record();
        record.setAid(aid);
        // 取款的交易金额应该是 负数
        record.setMoney(-money);
        oprecordDao.insert(record);
    }

    public void transfer(int aid1, int aid2, double money) throws BizException {
        withdraw(aid1, money);
        bankBiz1.deposit(aid2,money);  //NAVER
    }

}
